﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.IO;
using WebVideo.Areas.Admin.Models;
using System.Collections;
using System.Data.Linq.SqlClient;


namespace WebVideo.Areas.Admin.Models
{
    public class VideoModel
    {
        public int Id { get;set; }

        [Required(ErrorMessage = "Tên không được rỗng")]
        [StringLength(50, ErrorMessage = "Tên phải ít hơn {1} kí tự và lớn hơn {2} kí tự", MinimumLength = 5)]
        public string Name {get;set;}

        [Required(ErrorMessage = "URL Video không được rỗng")]
        [RegularExpression("(https?://(www\\.)?youtube\\.com/.*v=\\w+.*)|(https?://youtu\\.be/\\w+.*)|(.*src=.https?://(www\\.)?youtube\\.com/v/\\w+.*)|(.*src=.https?://(www\\.)?youtube\\.com/embed/\\w+.*)", ErrorMessage = "Bạn phải nhập URL ")]
        public string Video { get; set; }

        public int IdType { get; set; } // loai Video

        [Required(ErrorMessage = "Lượt view không được để trống")]
        [RegularExpression("^\\+?\\d+$", ErrorMessage = "Bạn phải nhập số")]
        public int Views { get; set; }

        public int  Viewed {get;set;}
        public DateTime Viewed_Date { get; set; }

        public string Criteria { get; set; } //  All|| Age, Sex, City, All

        public string IdBusiness { get; set; } //

        public int TypeQuestion { get; set; } // 2: trắc ngiệm, 1 tự luận

        public DateTime Date { set; get; }
         [Required(ErrorMessage = "MetaDescription không được để trống")]
         [StringLength(70, ErrorMessage = "MetaDescription phải ít hơn {1} kí tự và lớn hơn {2} kí tự", MinimumLength = 5)]
        public string MetaDesciption { get; set; }

         [Required(ErrorMessage = "MetaKeyWord không được để trống")]
         [StringLength(70, ErrorMessage = "MetaKeyWord phải ít hơn {1} kí tự và lớn hơn {2} kí tự", MinimumLength = 5)]
        public string MetaKeyWord { get; set; }

         public string Image { get; set; }

        public IEnumerable<EntityTypeVideo> DSTypeVideo
        {
            get
            {
                dbVideoDataContext db = new dbVideoDataContext();
                var query = from p in db.EntityTypeVideos
                            select p;
                return query.ToList();
            }
            set
            {

            }
        }
        public IEnumerable<EntityBusiness > DSBusiness
        {
            get
            {
                dbVideoDataContext db = new dbVideoDataContext();
                var query = from p in db.EntityBusinesses 
                            select p;
                return query.ToList();
            }
            set
            {

            }
        }

        // load
        public static IEnumerable<EntityVideo > LayDuLieu()
        {
            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntityVideos
                        orderby p.Date descending
                        select p;

            return query.ToList();
        }

        //insert
      
        #region "insert "
       
        public static void Insert(VideoModel  Mtnew)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            EntityVideo  tnew = new EntityVideo();
            tnew.Name = Mtnew.Name ;

            string video = Mtnew.Video.ToString();
            string[] arr = video.Split('=');
            tnew.Video = arr[1].ToString();

            tnew.IdType = Mtnew.IdType;
            tnew.Views = Mtnew.Views;
            tnew.Viewed = 0;
            tnew.Criteria = Mtnew.Criteria;
            tnew.IdBusiness = Mtnew.IdBusiness;
            tnew.TypeQuestion = Mtnew.TypeQuestion;
            tnew.Date = DateTime.Now;
            tnew.MetaDesciption = Mtnew.MetaDesciption;
            tnew.MetaKeyWord = Mtnew.MetaKeyWord;
            tnew.Status = 2;// ẩn video đi, chờ admin kích hoạt
            tnew.Image = Mtnew.Image;

            db.EntityVideos .InsertOnSubmit(tnew);
            db.SubmitChanges();
           
        }
        #endregion
        // edit
        #region "edit "
        
        public static VideoModel LayModel(int IdVideo, int loai)
        {
            VideoModel Mtnew = new VideoModel();


            dbVideoDataContext db = new dbVideoDataContext();

            EntityVideo tnew = db.EntityVideos.Where(p => p.Id == IdVideo).Single();

            Mtnew.Name = tnew.Name;
            Mtnew.Id = tnew.Id;
            if (loai == 0)
            {
                string video = "http://www.youtube.com/watch?v=" + tnew.Video.ToString();
                Mtnew.Video = video;
            }
            else
            {
                Mtnew.Video = tnew.Video;
            
            }
            Mtnew.TypeQuestion = (int) tnew.TypeQuestion;
            Mtnew.Views =(int) tnew.Views;
            Mtnew.Criteria = tnew.Criteria;
            Mtnew.IdBusiness = tnew.IdBusiness;
            Mtnew.MetaDesciption = tnew.MetaDesciption;
            Mtnew.MetaKeyWord = tnew.MetaKeyWord;
            Mtnew.Image = tnew.Image;
            return Mtnew;

        }
        public static string Edit(VideoModel Mtnew)
        {
            dbVideoDataContext db = new dbVideoDataContext();

            EntityVideo tnew = db.EntityVideos.Where(p => p.Id == Mtnew.Id).Single();

            tnew.Name = Mtnew.Name;
            string video = Mtnew.Video.ToString();
            string[] arr = video.Split('=');
            tnew.Video = arr[1].ToString();
            tnew.IdType = Mtnew.IdType;
            if (Mtnew.Views > tnew.Viewed)
            {
                tnew.Views = Mtnew.Views;
            }
    
            tnew.Criteria = Mtnew.Criteria;
            tnew.IdBusiness = Mtnew.IdBusiness;
            if (Mtnew.TypeQuestion != tnew.TypeQuestion)
            {
                tnew.Status = 2;
                tnew.TypeQuestion = Mtnew.TypeQuestion;
            }
            string image = null;
            if (Mtnew.Image != null)
            {
                image = tnew.Image;
                tnew.Image = Mtnew.Image;
            }
            tnew.MetaDesciption = Mtnew.MetaDesciption;
            tnew.MetaKeyWord = Mtnew.MetaKeyWord;

            db.SubmitChanges();
            return image;

        }
        #endregion
        // delete
        #region "Delete "
        public static string  Delete(int Id)
        {
            dbVideoDataContext db = new dbVideoDataContext();

            EntityVideo tnew = db.EntityVideos.Where(p => p.Id == Id).Single();
            db.EntityVideos.DeleteOnSubmit(tnew);
            db.SubmitChanges();
            return tnew.Image;

        }
        #endregion
        #region "hide "
        
        public static string Hide(int Id, int l)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            EntityVideo tnew = db.EntityVideos.Where(p => p.Id == Id).First();
            if (l == 1)
            {
                if (tnew.Views <= tnew.Viewed)
                {
                    return "error";
                }
                var likestr = string.Format("%{0}%", Id.ToString() + ",");
                var query = from p in db.EntityQuestions_MultipleChoices
                            where SqlMethods.Like(p.ListVideo, likestr)
                            select p;
                if (tnew.TypeQuestion == 2)
                {
                   // var query = db.EntityQuestions_MultipleChoices.Where(g => g.Type == 1 && g.IdVideo == Id);// câu hỏi trắc ngiệm dành cho hình ảnh
                    var query2 = db.EntityQuestions_MultipleChoices.Where(g => g.Type == 2 && g.IdVideo == Id);// câu hỏi trắc ngiệm bình thường
                    if (query.ToList().Count > 0 && query2.ToList().Count > 0)
                    {
                        tnew.Status = l;
                        db.SubmitChanges();
                        return "complete";
                    }
                }
                else
                {
                  //  var query = db.EntityQuestions_MultipleChoices.Where(g => g.Type == 1 && g.IdVideo == Id);// câu hỏi trắc ngiệm dành cho hình ảnh
                    var query2 = db.EntityQuestions_Essays.Where(g => g.IdVideo == Id);// câu hỏi tự luận dành cho hình ảnh
                    if (query.ToList().Count > 0 && query2.ToList().Count >0)
                    {
                        tnew.Status = l;
                        db.SubmitChanges();
                        return "complete";
                    }
                }
            }
            else
            {
                tnew.Status = l;
                db.SubmitChanges();
                return "complete";
            }
            return "error";
        }
        #endregion

    }
}
